<?php
namespace Opencart\Admin\Controller\Extension\OpencartcityTranslate\Module;
class OpcTranslate extends \Opencart\System\Engine\Controller {
  public function install(): void {
    $this->load->model('setting/event');

    $events = array(
      array(
        'trigger' => 'admin/view/common/header/after',
        'action' => 'extension/opencartcity_translate/module/opc_translate|addTranslate'
      ),
      array(
        'trigger' => 'catalog/view/common/header/after',
        'action' => 'extension/opencartcity_translate/module/opc_translate'
      )
    );

    foreach ($events as $event) {
      $data = array(
        'code' => 'opc_translate',
        'description' => '',
        'trigger' => $event['trigger'],
        'action' => $event['action'],
        'status' => 1,
        'sort_order' => 0
      );

      $this->model_setting_event->addEvent($data);
    }
  }

  public function uninstall(): void {
    $this->load->model('setting/event');

    $this->model_setting_event->deleteEventByCode('opc_translate');
  }

  public function addTranslate(&$route, &$data, &$code) {
    if ($this->config->get('module_opc_translate_admin_position') == 1) {
      $code = str_replace('<ul class="nav navbar-nav">', '<div style="position: absolute; left: 15%;">' . $this->config->get("module_opc_translate_default_code") . '</div><ul class="nav navbar-nav">', $code);
    } elseif ($this->config->get('module_opc_translate_admin_position') == 2) {
      $code = str_replace('<ul class="nav navbar-nav">', '<div>' . $this->config->get("module_opc_translate_default_code") . '</div><ul class="nav navbar-nav">', $code);
    } else {
      $code = str_replace('</span></a></li>', '</span></a></li><li style="margin-top: 5%;">' . $this->config->get("module_opc_translate_default_code") . '</li>', $code);
    }
  }

  public function index(): void {
    $this->load->language('extension/opencartcity_translate/module/opc_translate');

    $this->document->setTitle($this->language->get('heading_title'));

    $data['breadcrumbs'] = [];

    $data['breadcrumbs'][] = [
      'text' => $this->language->get('text_home'),
      'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
    ];

    $data['breadcrumbs'][] = [
      'text' => $this->language->get('text_extension'),
      'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module')
    ];

    $data['breadcrumbs'][] = [
      'text' => $this->language->get('heading_title'),
      'href' => $this->url->link('extension/opencartcity_translate/module/opc_translate', 'user_token=' . $this->session->data['user_token'])
    ];

    $data['user_guide'] = $this->url->link('extension/opencartcity_translate/module/opc_translate|user_guide', 'user_token=' . $this->session->data['user_token']);
    $data['save'] = $this->url->link('extension/opencartcity_translate/module/opc_translate|save', 'user_token=' . $this->session->data['user_token']);
    $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module');

    $opc_module_config = array(
      'status',
      'admin',
      'catalog',
      'admin_position',
      'catalog_position',
    );

    foreach ($opc_module_config as $key => $value) {
      if (isset($this->request->post['module_opc_translate_'.$value])) {
        $data['module_opc_translate_'.$value] = $this->request->post['module_opc_translate_'.$value];
      } else {
        $data['module_opc_translate_'.$value] = $this->config->get('module_opc_translate_'.$value);
      }
    }

    $data['header'] = $this->load->controller('common/header');
    $data['column_left'] = $this->load->controller('common/column_left');
    $data['footer'] = $this->load->controller('common/footer');

    $this->response->setOutput($this->load->view('extension/opencartcity_translate/module/opc_translate', $data));
  }

  public function user_guide(): void {
    $this->document->setTitle('Opencart Google Translate User Guide');

    $data['cancel'] = $this->url->link('extension/opencartcity_translate/module/opc_translate', 'user_token=' . $this->session->data['user_token']);

    $data['user_token'] = $this->session->data['user_token'];

    $data['base_url'] = HTTP_CATALOG;

    $data['header'] = $this->load->controller('common/header');

    $data['column_left'] = $this->load->controller('common/column_left');

    $data['footer'] = $this->load->controller('common/footer');

    $this->response->setOutput($this->load->view('extension/opencartcity_translate/module/opc_translate_user_guide', $data));
  }

  public function save(): void {
    $this->load->language('extension/opencartcity_translate/module/opc_translate');

    $json = [];

    if (!$this->user->hasPermission('modify', 'extension/opencartcity_translate/module/opc_translate')) {
      $json['error'] = $this->language->get('error_permission');
    }

    if (!$json) {
      $this->request->post['module_opc_translate_default_code'] = '<div id="google_translate_element"></div><script type="text/javascript">
      function googleTranslateElementInit() {
        new google.translate.TranslateElement({pageLanguage: "en", layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, "google_translate_element");
      }
      </script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>';

      $this->load->model('setting/setting');

      $this->model_setting_setting->editSetting('module_opc_translate', $this->request->post);

      $json['success'] = $this->language->get('text_success');
    }

    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));
  }
}
